﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Errors_Default : BasePage
{
    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Developer }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dtFrom.SelectedDate = DateTime.Today.AddMonths(-1);
            DataBind();
        }
    }

    private Acms.Exceptions.LogFactory _Factory;
    protected Acms.Exceptions.LogFactory Factory
    {
        get
        {
            if (_Factory == null)
            {
                _Factory = new Acms.Exceptions.LogFactory();
            }
            return _Factory;
        }
    }

    protected void dsErrors_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(Factory.Search(AcmsContext.SiteId, Server.MapPath("~/../"), ErrorTypes, Urls, From, To));
    }

    protected void dsErrorTypes_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(Factory.GetExceptionTypesBySiteIdAndRoot(AcmsContext.SiteId, Server.MapPath("~/../")));
    }

    protected void dsUrls_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(Factory.GetUrlsBySiteIdAndRoot(AcmsContext.SiteId, Server.MapPath("~/../")));
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            grid.CurrentPageIndex = 0;
            Urls = ddlUrl.SelectedValue;
            ErrorTypes = ddlErrorType.SelectedValue;
            From = dtFrom.SelectedDate;
            To = dtTo.SelectedDate;
            grid.Rebind();
        }
    }

    protected void grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Details")
        {
            details.PageIndex = e.Item.ItemIndex;
            pnlDetailsCommand.Visible = details.Visible = true;
            grid.Visible = pnlForm.Visible = false;
        }
    }

    protected void btnDetailsBack_Click(object sender, EventArgs e)
    {
        pnlDetailsCommand.Visible = details.Visible = false;
        grid.Visible = pnlForm.Visible = true;
    }

    protected void lnk_DataBinding(object sender, EventArgs e)
    {
        var lnk = sender as HyperLink;
        var log = ((IDataItemContainer)lnk.NamingContainer).DataItem as Acms.Exceptions.Log;
        if (log != null)
        {
            string spacer = "";
            if(!string.IsNullOrEmpty(log.Querystring))
            {
                if(log.AcmsUrl != null && log.AcmsUrl.Contains("?"))
                    spacer = "&";
                else
                    spacer = "?";
            }
            lnk.Text = lnk.NavigateUrl = string.Concat("http://", log.ServerName, log.ApplicationRoot, log.AcmsUrl ?? log.Url, spacer, log.Querystring);
        }
    }

    public string Urls { get; set; }
    public string ErrorTypes { get; set; }
    public DateTime? From { get; set; }
    public DateTime? To { get; set; }

    protected override object SaveViewState()
    {
        ViewState["Urls"] = Urls;
        ViewState["ErrorTypes"] = ErrorTypes;
        ViewState["From"] = From;
        ViewState["To"] = To;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        Urls = ViewState["Urls"] as string;
        ErrorTypes = ViewState["ErrorTypes"] as string;
        From = ViewState["From"] as DateTime?;
        To = ViewState["To"] as DateTime?;
    }
}
